<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <el-button @click="addVisible = true">新增</el-button>
      </div>
    </template>
    <el-table :data="adminList.list" style="width: 100%">
      <el-table-column prop="id" label="id"/>
      <el-table-column prop="username" label="管理员账户"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="realname" label="姓名"/>
      <el-table-column label="状态">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              inline-prompt
              :active-value=1
              active-text="正常"
              :inactive-value=0
              inactive-text="禁用"
              @change="(status) => {updateStatusInfo(scope.row.id,status)}"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="showEdit(scope.row.id)">编辑</el-button>
          <el-popconfirm title="你确定要重置密码吗？" confirm-button-text="确认" cancel-button-text="取消"
                         @confirm="resetPwd(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger">重置密码</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-row class="row-bg" justify="space-evenly">
        <el-col :span="6">
          <el-pagination layout="prev, pager, next" :total="adminList.total" :page-size="adminList.pageSize"
                         @current-change="getAdminList"/>
        </el-col>
      </el-row>
    </template>
  </el-card>


  <!-- START 修改对话框 -->
  <el-dialog v-model="updateVisible" title="编辑管理员信息" align-center style="width: 300px">
    <el-form :model="updateInfo">
      <el-form-item label="手机号" style="width: 250px">
        <el-input v-model="updateInfo.phone" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="邮箱" style="width: 250px">
        <el-input v-model="updateInfo.email" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="姓名" style="width: 250px">
        <el-input v-model="updateInfo.realname" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateVisible = false">取消</el-button>
        <el-button type="primary" @click="subUpdate">提交</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- END 修改对话框 -->


  <!-- START 新增对话框 -->
  <el-dialog v-model="addVisible" title="编辑管理员信息" align-center style="width: 300px">
    <el-form :model="addInfo">
      <el-form-item label="用户名" style="width: 250px">
        <el-input v-model="addInfo.username" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="手机号" style="width: 250px">
        <el-input v-model="addInfo.phone" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="邮箱" style="width: 250px">
        <el-input v-model="addInfo.email" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="姓名" style="width: 250px">
        <el-input v-model="addInfo.realname" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="subAdd">提交</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- END 新增对话框 -->
</template>

<script setup>
import {ref} from "vue";
import adminApi from "@/api/AdminApi.js";
import {ElMessage} from "element-plus";

const adminList = ref({
  pageNum: 1,
  pageSize: 5,
  total: 10,
  list: []
});

const updateVisible = ref(false)
const updateInfo = ref({})
const addVisible = ref(false)
const addInfo = ref({
  realname:'',
  email:'',
  phone:'',
  username:''
})

// 获得全部管理员
const getAdminList = (pageNum) => {
  adminApi.getAllByPage(pageNum, 5)
      .then(resp => {
        if (resp.code === 10000) {
          adminList.value = resp.data
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 修改管理员信息
const updateStatusInfo = (id, status) => {
  let date = {id, status}
  adminApi.updateOneInfo(date)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 打开编辑弹出层
const showEdit = (id) => {
  adminApi.getById(id)
      .then(resp => {
        if (resp.code === 10000) {
          updateInfo.value = resp.data
          updateVisible.value = true
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 重置密码
const resetPwd = (id) => {
  adminApi.resetPwd(id)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
          getAdminList(adminList.value.pageNum)
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 提交更新
const subUpdate = () => {
  adminApi.updateOneInfo(updateInfo.value)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
          getAdminList(adminList.value.pageNum)
          updateVisible.value = false
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 提交新增
const subAdd = () => {
  adminApi.addInfo(addInfo.value)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
          getAdminList(adminList.value.pageNum)
          addVisible.value = false
          addInfo.value = {
            realname:'',
            email:'',
            phone:'',
            username:''
          }
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

getAdminList(1)
</script>

<style scoped>

</style>